package com.ld.xuanKe.controller;

import com.alibaba.excel.EasyExcel;
import com.ld.xuanKe.entity.XsTeachingClass;
import com.ld.xuanKe.entity.XsTeachingClassDisplay;
import com.ld.xuanKe.service.XsTeachingClassService;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ProjectName xuanKe
 * @ClassName XsTeachingClassAction
 * @Author 刘邓
 * @Date 2023-12-22 20:42
 */
@Controller
@Scope("prototype")
public class XsTeachingClassAction implements ModelDriven<XsTeachingClass> {
	private XsTeachingClass xsTeachingClass;
	@Resource
	private XsTeachingClassService xsTeachingClassService;
	public XsTeachingClass getModel() {
		xsTeachingClass = new XsTeachingClass();
		return xsTeachingClass;
	}
	//excel导出指定教学班学生的信息
	public void downLoadMyStudents() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			HttpServletResponse resp = ServletActionContext.getResponse();
			List<XsTeachingClassDisplay> myStudents = xsTeachingClassService.displayListByClassId(xsTeachingClass.getTeachingclassId());
			resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			resp.setCharacterEncoding("utf-8");
			// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
			String fileName = URLEncoder.encode("学生信息", "UTF-8").replaceAll("\\+", "%20");
			resp.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
			EasyExcel.write(resp.getOutputStream(), XsTeachingClassDisplay.class).sheet("学生信息").doWrite(myStudents);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
